#include <iostream>
#include <string>
#include <vector>
#include <windows.h>
#include <shellapi.h>
#include <shlobj.h>
#include <direct.h>
#include <cstring>

// 显示帮助信息
void showHelp();
void showGifHelp();

int main(int argc, char* argv[]) {
    // 确定可执行文件路径
    char exePath[MAX_PATH];
    GetModuleFileName(NULL, exePath, MAX_PATH);
    std::string exeDir = exePath;
    size_t lastSlash = exeDir.find_last_of("/\\");
    if (lastSlash != std::string::npos) {
        exeDir = exeDir.substr(0, lastSlash + 1);
    }
    
    std::string goScrotPath = exeDir + "go-scrot.exe";
    std::string gifRecPath = exeDir + "gif-rec.exe";

    // 检查是否存在 --gif 参数
    bool isGifMode = false;
    bool showHelpFlag = false;
    
    // 构建参数列表
    std::vector<std::string> args;
    for (int i = 1; i < argc; ++i) {
        std::string arg = argv[i];
        
        if (arg == "--gif") {
            isGifMode = true;
            // 跳过 --gif 参数，不传递给子命令
            continue;
        }
        
        if (arg == "-h" || arg == "--help") {
            showHelpFlag = true;
        }
        
        args.push_back(arg);
    }
    
    // 显示帮助信息
    if (showHelpFlag) {
        if (isGifMode) {
            showGifHelp();
        } else {
            showHelp();
        }
        return 0;
    }
    
    // 构建命令行
    std::string command;
    if (isGifMode) {
        command = "\"" + gifRecPath + "\"";
    } else {
        command = "\"" + goScrotPath + "\"";
    }
    
    // 添加参数
    for (const auto& arg : args) {
        command += " " + arg;
    }
    
    // 执行命令
    std::cout << "执行命令: " << command << std::endl;
    int result = system(command.c_str());
    
    if (result != 0) {
        std::cerr << "命令执行失败，返回代码: " << result << std::endl;
        return result;
    }
    
    return 0;
}

void showHelp() {
    std::cout << "scrot - 屏幕截图和GIF录制工具" << std::endl;
    std::cout << "用法:" << std::endl;
    std::cout << "  scrot [go-scrot选项]          - 使用截图功能" << std::endl;
    std::cout << "  scrot --gif [gif-rec选项]     - 使用GIF录制功能" << std::endl;
    std::cout << std::endl;
    std::cout << "go-scrot选项:" << std::endl;
    std::cout << "  -d int             延迟截图时间" << std::endl;
    std::cout << "  -f string          图像格式[png,jpg,jpeg,bmp,webp,tiff] (default \"png\")" << std::endl;
    std::cout << "  -h                 显示帮助" << std::endl;
    std::cout << "  -o string          输出文件路径" << std::endl;
    std::cout << "  -s                 选择截图区域" << std::endl;
    std::cout << std::endl;
    std::cout << "使用 'scrot --gif -h' 查看GIF录制选项" << std::endl;
}

void showGifHelp() {
    // 调用gif-rec.exe --help获取实际帮助信息
    char exePath[MAX_PATH];
    GetModuleFileName(NULL, exePath, MAX_PATH);
    std::string exeDir = exePath;
    size_t lastSlash = exeDir.find_last_of("/\\");
    if (lastSlash != std::string::npos) {
        exeDir = exeDir.substr(0, lastSlash + 1);
    }
    
    std::string gifRecPath = exeDir + "gif-rec.exe";
    std::string command = "\"" + gifRecPath + "\" --help";
    
    std::cout << "GIF屏幕录制工具 - 使用msf_gif库实现" << std::endl;
    std::cout << "----------------------------------------" << std::endl;
    std::cout << "执行命令: " << command << std::endl;
    system(command.c_str());
}